import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/2/22 21:01
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        TreeNode t2 = new TreeNode(2, new TreeNode(3), new TreeNode(4)), t5 = new TreeNode(5, null, new TreeNode(6)),
                t1 = new TreeNode(1, t2, t5);
        FormalSolution formalSolution = new FormalSolution();
        formalSolution.flatten(t1);
    }
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<TreeNode>();
        preorderTraversal(root, list);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }
    }
    public void preorderTraversal(TreeNode root, List<TreeNode> list) {
        // 前序遍历
        if (root != null) {
            list.add(root);
            preorderTraversal(root.left, list);
            preorderTraversal(root.right, list);
        }
    }
}
